<?php

namespace Application\Service\Sitemap;

use Zend\Serializer\Serializer;
use Application\Service\Sitemap\url as url;

class SitemapService
{
    protected $Url;
    private static $xml;
    private $urls = array();

    public function __construct( url $UrlService )
    {
        $this->Url = $UrlService;

        $this->xml = new \XmlWriter();
        $this->xml->openMemory();
        $this->xml->startDocument('1.0', 'UTF-8');
        $this->xml->setIndent(true);
        $this->xml->startElement('urlset');
        $this->xml->writeAttribute('xmlns','http://www.sitemaps.org/schemas/sitemap/0.9');
    }

    public function objToXML($obj) {
        $this->getObject2XML($this->xml, $obj);
 
        $this->xml->endElement();

        return $this->xml;
    }

    private function stripNamespaceFromClassName($obj)
    {
        $classname = get_class($obj);

        if (preg_match('@\\\\([\w]+)$@', $classname, $matches)) {
            $classname = $matches[1];
        }

        return $classname;
    }

    private function getObject2XML( \XMLWriter $xml, $data) {

        foreach($data as $key => $value) {

            if(is_object($value)) {
                // $xml->startElement($key);

                $xml->startElement( $this->stripNamespaceFromClassName($value) );
                $this->getObject2XML($xml, $value);
                $xml->endElement();
                continue;
            }
            else if(is_array($value)) {
                $this->getArray2XML($xml, $key, $value);
            }
 
            if (is_string($value) || is_numeric($value)) {
                $xml->writeElement($key, $value);
            }
        }
    }

    private function getArray2XML( \XMLWriter $xml, $keyParent, $data ) {
        foreach($data as $key => $value) {
            if (is_string($value)) {
                $xml->writeElement($keyParent, $value);
                continue;
            }
 
            if (is_numeric($key)) {
                $xml->startElement($keyParent);
            }
 
            if(is_object($value)) {
                $this->getObject2XML($xml, $value);
            }
            else if(is_array($value)) {
                $this->getArray2XML($xml, $key, $value);
                continue;
            }
 
            if (is_numeric($key)) {
                $xml->endElement();
            }
        }
    }

    public function xmlToObj($xmlString) {
        return simplexml_load_string($xmlString);
    }


    public function addItem($loc, $priority = null, $lastmod = null, $changefreq = null)
    {
        $u = new $this->Url;

        $u->setLoc($loc);
        $u->setPriority($priority);
        $u->setLastmod($lastmod);
        $u->setChangefreq($changefreq);

        $this->urls[] = $u;
    }

    public function getItems() {
        return $this->urls;
    }

    public function saveToFile( $xml ) {
        file_put_contents('sitemap.xml', $xml->flush(true) );
        copy('sitemap.xml', 'public/sitemap.xml');
        unlink( 'sitemap.xml' );
    }

}